$(document).ready(function(){

})
//获取优惠券二维码图片
function getCouponNumBarcode(couponNum){
	var couponNumBarcode='';
    $.ajax({
    	  url: "/csfy/getUrlBarcode.do",
    	  async:false,
    	  dataType:"json",
    	  data:{
    		  "url":"checkQRUser.do?couponNum=" + couponNum
    	  },
    	  success: function(data){
    		couponNumBarcode = data.rows;
    	  }  
    	});
    return couponNumBarcode;
}
//获取当前用户信息
function getPerSonInfo(){
	var url = "/csfy/wx/getPresonInfo.do";
	var personInfo='';
    $.ajax({
        type:"post",
        url:url,
        async:false,
        data:{},
        dataType:"json",
        success:function (data) {
        	personInfo = data.data;
        }
    });
    return personInfo;
}
//根据id获取送检单详情
function getSampleBaseBySampleId(sampleId){
	var sampleInfo='';
    $.ajax({
    	type:"post",
    	  url: "/csfy/wx/getSampleBaseDetailInfo.do",
    	  async:false,
    	  dataType:"json",
    	  data:{
    		  "sampleBaseId":sampleId
    	  },
    	  success: function(data){
    		  sampleInfo = data.rows;
    	  }  
    	});
    return sampleInfo;
}
function doQuery(couponNum,sampleId){
//            var couponNumBarcode=getCouponNumBarcode(couponNum);
//      		var barcodeImgBase64Code ='data:image/jpg;base64,'+couponNumBarcode;
//      		$('#barcodeImg').attr('src',barcodeImgBase64Code);
            var personInfo=getPerSonInfo();
            makeupPersonInfoInfoView(personInfo);
            var sampleInfo=getSampleBaseBySampleId(sampleId);
            makeupSampleInfoInfoView(sampleInfo);
 

        }



var personInfoKeyMappings={
		"sex":"性别",
		"name":"姓名",
		"nation":"民族",
		"birth":"出生日期",
		"address":"户籍地址",
		"id":"身份证号",
		"localaddr":"常住地址",
		"presonType":"认证类型"
}

var personInfoValueMappings={
		"presonType":{
			"1":"本人为长沙户口",
			"2":"配偶为长沙户口",
			"3":"拥有长沙居住证",
			"4":"军人及军人配偶",
			"5":"其他类型",
		}
}
var niftySampleInfoKeyMappings={
		"customerName": "送检单位",
		"sampleName": "姓名",
		"dnaMotherName": "姓名拼音",
		"bloodDate": "抽血时间",
		"sampleSex": "性别",
		"tubeType": "样品管类型",
		"takeBloodBatcbNum": "采血管批号",
		"oldSampleNum": "原样品编号",
		"gestationalWeeks": "孕周",
		"idCard": "身份证",
		"motherPhoneNum": "联系电话",
		"husbandPhoneNum": "丈夫电话",
		"projectType": "项目类型",
		// "isNeedOtherChromReport": "是否需要其他染色体附加正式报告",
		// "isNeedReportOtherResult": "是否需要报告清单提示其它结果",
		"applyTime": "申请时间",
		"birthDate": "出生日期",
		"motherAge": "年龄",
		"bodyWeight": "体重",
		"bodyHeight": "身高",
		"lmp": "末次月经",
		"edd": "预产期",
		"clinicalDiagnosis": "临床诊断",
		"doctorName": "医生姓名",
		"hosptialNum": "门诊号/住院号",
		"sampleRecipientEmail": "通讯地址（或电子邮箱）",
		"pregnancy": "本次妊娠方式",
		"adosculation": "是否IVF",
		"personalReproductiveHistory": "生育史",
		"personalReproductiveHistory1": "孕次",
		"personalReproductiveHistory2": "产次",
		"familyProduce": "家族史",
		"abnormalPregnancyHistory": "不良孕产史",
		"naturalAbortionNum": "自然流产次数",
		"stillbirthNum": "死胎次数",
		"neonatalDeathNum": "新生儿死亡次数",
		"oafNum": "畸形儿次数",
		"ntd": "神经管缺陷筛查",
		"gravidaKaryotype": "孕妇染色体核型",
		"husbandKaryotype": "丈夫染色体核型",
		"allogeneticTransfusion": "一年内异体输血",
		"cellularImmunity": "四周内异体细胞免疫",
		"surgicalTransplantation": "移植手术",
		"stemCell": "干细胞治疗",
		"sampleType": "样本类型",
		"btypeUltrasonic": "辅助检查(重要)",
		"ultrasonography": "备注",
		"screeningModel": "筛查模式",
		"motherSerumRisk": "母血清筛查风险率",
		"trisomy21": "21三体综合征： 1/",
		"trisomy18": "18三体综合征： 1/",
		"ntGestationalWeeks": "超声检测孕周",
		"ntGestationalWeeks2": "天",
		"ntValuent": "测定值",
		"productName": "产品名称",
		"productCode": "产品编码",
		"departments": "科室",
		"address": "现居住址",
		"sampleTypeOther": "样本其他类型的名称",
		"personalHistory": "既往病史",
		"phoneNum": "联系电话",
		"nationaltily": "民族",
		"nativePlace": "籍贯",
		"bedNum": "床号",
		"clinicalExamination": "临床检查",
		"motherName": "母亲姓名",
		"addressStreet": "详细地址",
		"pregnancyInfection": "母亲怀孕期感染",
		"consanguineousMarriage": "父母近亲结婚",
		"medicationHistory": "用药史（氨基糖苷类药物）",
		"familyDeafness": "耳聋家族史",
		"earDeformity": "耳部畸形（包括小耳、内耳畸形等）",
		"isCharge": "收费类型",
		"guardianShip": "监护人姓名",
		"samplePhoneNum": "其他联系电话",
		"clinicalManifestation": "临床表现",
		"apgar": "apgar评分",
		"suckler72": "是否充分哺乳",
		"deliveryMode": "分娩方式",
		"ntValue":"NT测定值（mm）",
		"bloodCode":"采血卡编号",
		"notes":"备注"
	}
var niftySampleInfoValueMappings={
		"tubeType": {
			"0": "STRECK",
			"1": "EDTA",
			"2": "K牌采血管",
			"3": "T管",
			"4": "K牌采血管新型试生产(测试专用)",
			"5": "G牌采血管"
		},
		"projectType": {
			"0": "新OSCAR样板",
			"1": "B超畸形",
			"2": "加深测序",
			"3": "无创CNV模拟",
			"4": "O2O-5自然日",
			"5": "O2O-14自然日"
		},
		"isNeedOtherChromReport": {
			"0": "否",
			"1": "是"
		},
		"isNeedReportOtherResult": {
			"0": "否",
			"1": "是"
		},
		"pregnancy": {
			"0": "自然受孕",
			"1": "促排卵",
			"2": "宫内人工受精"
		},
		"adosculation": {
			"0": "否",
			"1": "是"
		},
		"abnormalPregnancyHistory": {
			"0": "无",
			"1": "有"
		},
		"ntd": {
			"0": "未做",
			"1": "已做（高风险）",
			"2": "已做（低风险）"
		},
		"gravidaKaryotype": {
			"0": "未做",
			"1": "正常",
			"2": "异常"
		},
		"husbandKaryotype": {
			"0": "未做",
			"1": "正常",
			"2": "异常"
		},
		"allogeneticTransfusion": {
			"0": "无",
			"1": "有（不接受）"
		},
		"cellularImmunity": {
			"0": "无",
			"1": "有（不接受）"
		},
		"surgicalTransplantation": {
			"0": "无",
			"1": "有（不接受）"
		},
		"stemCell": {
			"0": "无",
			"1": "有（不接受）"
		},
		"sampleType": {
			"30": "华大保存液",
			"14": "FTA卡",
			"01": "全血",
			"03": "DNA",
			"05": "干血片",
			"08": "口腔拭子",
			"18": "唾液",
			"32": "其他",
			"1": "全血"
		},
		"btypeUltrasonic": {
			"0": "正常(单胎活)",
			"1": "正常(双胎活)",
			"2": "正常(多胎活)",
			"3": "异常(单胎)",
			"4": "异常(双胎)",
			"5": "异常(多胎)"
		},
		"screeningModel": {
			"0": "未做",
			"1": "nt筛查",
			"2": "早孕期筛查",
			"3": "中孕期筛查",
			"4": "早中孕期联合筛查"
		},
		"motherSerumRisk": {
			"0": "高风险",
			"1": "低风险",
			"2": "临界风险"
		},
		"sampleSex": {
			"0": "未知",
			"1": "男",
			"2": "女"
		},
		"pregnancyInfection": {
			"0": "无",
			"1": "有"
		},
		"consanguineousMarriage": {
			"0": "无",
			"1": "有"
		},
		"medicationHistory": {
			"0": "否",
			"1": "是"
		},
		"familyDeafness": {
			"0": "无",
			"1": "有"
		},
		"earDeformity": {
			"0": "否",
			"1": "是"
		},
		"isCharge": {
			"1": "免费",
			"2": "自费"
		},
		"suckler72":{
			"0": "否",
			"1": "是"
		},
		"deliveryMode":{
			"0": "平产",
			"1": "剖腹产"
		}
	}

function makeupSampleInfoInfoView(sampleInfo){
	var sampleInfoNode = document.getElementById("sampleInfo"); 
	var couponBarcodeNode = document.getElementById("couponBarcode"); 
	var statusTitleNode = document.getElementById("statusTitle"); 
	statusTitleNode.innerText=sampleInfo.productName;
	if(sampleInfo["couponNum"]!=null){
	    var couponNumBarcode=getCouponNumBarcode(sampleInfo["couponNum"]);
		var barcodeImgBase64Code ='data:image/jpg;base64,'+couponNumBarcode;
//		var imgNode = document.createElement("img");
//		imgNode.setAttribute("src",barcodeImgBase64Code);
//		imgNode.setAttribute("style","width:100px;height:100px;display:block;margin:auto;") ;
  		$('#barcodeImg').attr('src',barcodeImgBase64Code);
		
		
		var clickNode = document.createElement("div");
		clickNode.onclick=function (){
            $("#fade").css('display','block');
            $("#tipDia").css('display','block');
    	}
		clickNode.setAttribute("style","color: blue;text-align: center;") ; 
		clickNode.innerText="查看免/自费券二维码";
//		couponBarcodeNode.appendChild(imgNode);
		couponBarcodeNode.appendChild(clickNode);
	}else{
		var imgNode = document.createElement("div");
		imgNode.innerText="未使用免费券";
		couponBarcodeNode.appendChild(imgNode);
	}
	

	
	for (var key in sampleInfo){
		var realKey=niftySampleInfoKeyMappings[key];
		if(null==realKey){
			continue;
		}
		var child=null;
		var valueMappings=niftySampleInfoValueMappings[key];
		if(null!=valueMappings){
			var realValue=valueMappings[sampleInfo[key]];
			// if(null==realValue){
			// 	realValue="其他";
			// }
			child=getItemElement(realKey,realValue,"");
		}else{
			child=getItemElement(realKey,sampleInfo[key],"");
		}
		if(null!=child){
			sampleInfoNode.appendChild(child);
		}
	}
	//未抽血可以删除
	var sampleStatus=sampleInfo["sampleStatus"];
	if("0"==sampleStatus){
		var operationAreaNode = document.getElementById("operationArea"); 
		var deleteNode = document.createElement("div");
		deleteNode.className='dbutton';
		deleteNode.innerText="删除送检单";
		deleteNode.onclick=function (){
            swal(  
                    {title:"您确定要删除这条送检单吗？",
                    text:"删除后将无法恢复，请谨慎操作！",  
                    type:"info",  
                    showCancelButton:true,  
                    confirmButtonColor:"#428bca",  
                    confirmButtonText:"确定",  
                    cancelButtonText:"取消",  
                    closeOnConfirm:false,  
                    closeOnCancel:false  
                    },  
                    function(isConfirm,param,obj)  
                    {  
                        if(isConfirm)  
                        {  
                		  $.ajax({
          		    	  url: "/csfy/wx/deleteSampleBaseBySampleBaseId.do",
          		    	  async:false,
          		    	  dataType:"json",
          		    	  data:{
          		    		  "sampleBaseId":sampleInfo.sampleBaseId
          		    	  },
          		    	  success: function(data){
                              swal({title:"删除成功！",   
                                  type:"success"},function(){
                                	  window.history.go(-1);
                                  }) 
          		    	  } 
          		    	});
 
                        }  
                        else{  
                            swal({title:"已取消",  
                                type:"success"})  
                        }  
                    }  
                    );

    	}
		operationAreaNode.appendChild(deleteNode);
	}
}

function makeupPersonInfoInfoView(personInfo){
	var personInfoNode = document.getElementById("personInfo"); 
	var idCardImgNode = document.getElementById("idCardImg");
	var imgurl=personInfo.cardFrontImgUrl;
	if(null==imgurl){
		imgurl=personInfo.residencePermitImgUrl;
	}
	var imgNode = document.createElement("img");
	imgNode.className='idcard-photo';
	imgNode.setAttribute("style","width:95%;height:200px;border:solid 1px;") ;
	idCardImgNode.appendChild(imgNode);
	//图片脱敏发起查看请求
	$.ajax({
		url:'/csfy/coupon/getPicForGc.do',
		type:'GET',
		data:{fileName:imgurl},
		dataType: 'json',
		async:false,
		success:function (data) {
			imgNode.setAttribute("src","data:image/jpeg;base64," + data);
		}
	});
	
	personInfo.localaddr=personInfo.province+personInfo.city+personInfo.country+personInfo.town;
	for (var key in personInfo){
		var realKey=personInfoKeyMappings[key];
		if(null==realKey){
			continue;
		}
		var child=null;
		var valueMappings=personInfoValueMappings[key];
		if(null!=valueMappings){
			var realValue=valueMappings[personInfo[key]];
			// if(null==realValue){
			// 	realValue="其他";
			// }
			child=getItemElement(realKey,realValue,"");
		}else{
			child=getItemElement(realKey,personInfo[key],"");
		}
		if(null!=child){
			personInfoNode.appendChild(child);
		}
	    }
	
}
function getItemElement(name,value,style){
	if(null==value){
		return null;
	}
	var itemNode = document.createElement("div");
	itemNode.className='sheet_text';
	if(null!=style&&''!=style)
	itemNode.setAttribute("style",style);
	
	var itemNameNode = document.createElement("div");
	itemNameNode.className='sheet_label';
	itemNameNode.innerText=name;
	var itemValueNode = document.createElement("div");
	itemValueNode.className='sheet_value';
	itemValueNode.innerText=value;
	
	itemNode.appendChild(itemNameNode);
	itemNode.appendChild(itemValueNode);
	return itemNode;
	
}